﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WXApi.Model;
using WxTenpay.Service;
using WxTenpay.Service.Model;

namespace WXApi.Controllers.SysApi
{

    /// <summary>
    /// 获取单位、部门数据列表
    /// </summary>
    [Route("[controller]/[action]")]
    [ApiController]
    [Action]
    public class SysCompanyController : ReqDataControllerBase
    {
        private readonly ISysCompany syscompany;
        private readonly ISysDepartment sysdepartment;

        private readonly ISysLog syslog;
        private readonly ISysLoginLog sysloginlog;

        public SysCompanyController(ISysLog _syslog, ISysLoginLog _sysloginlog, ISysCompany _syscompany, ISysDepartment _sysdepartment)
        {
            syslog = _syslog;
            syscompany = _syscompany;
            sysdepartment = _sysdepartment;
            sysloginlog = _sysloginlog;
        }



        #region  获取树形菜单列表
        /// <summary>
        /// 新增单位信息 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult GetCompanyList()
        {
            try
            {
                var data = syscompany.GetList().Select(x =>
                {

                    return new
                    {
                        id = x.GuId,
                        label = x.CompanyName,
                    };
                });
                return Success(data);
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "SysCompany/GetCompanyList", Content = ex.ToString(), Name = "获取单位信息" };
                return ErrorLog(model);
            }
        }


        /// <summary>
        /// 获取单位部们信息 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult GetDepartment([FromBody] PageModel.PageParm parm)
        {
            try
            {
                var data = sysdepartment.GetPages(parm, x => x.CompanyGuId == parm.data.ToString(), x => x.Id, 1);
                return Success(data);
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "SysCompany/GetDepartment", Content = ex.ToString(), Name = "获取单位部门信息" };
                return ErrorLog(model);
            }
        }
        #endregion

        /// <summary>
        /// 新增单位信息 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult AddCompany([FromBody] SysCompanyEntity entity)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(entity.GuId))
                {     //新增

                    if (syscompany.GetModel(x => x.CompanyName == entity.CompanyName) == null)
                    {
                        entity.Create();

                        syscompany.Add(entity);

                        SuccessLogDto loginmodel = new SuccessLogDto();
                        loginmodel.data = "操作成功";
                        loginmodel._sysloginlog = sysloginlog;
                        loginmodel.loginlogmodel = new  SysLoginLogEntity { Type=1, Content = "新增单位信息" };
                        return SuccessLog(loginmodel);
                    }
                    else
                        return Error("对不起单位名称已存在！");

                }
                else
                {   //修改信息
                    var model = syscompany.GetModel(x => x.GuId == entity.GuId);
                    if (model != null)
                    {
                        model.CompanyName = entity.CompanyName;
                        syscompany.Update(model);

                    }
                    SuccessLogDto loginmodel = new SuccessLogDto();
                    loginmodel.data = "操作成功";
                    loginmodel._sysloginlog = sysloginlog;
                    loginmodel.loginlogmodel = new SysLoginLogEntity { Type = 2, Content = "修改单位信息" };
                    return SuccessLog(loginmodel);
                }

                
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "SysCompany/AddCompany", Content = ex.ToString(), Name = "新增单位信息" };
                return ErrorLog(model);
            }
        }


        /// <summary>
        /// 新增单位部门信息 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult AddDepartment([FromBody] SysDepartmentEntity entity)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(entity.GuId))
                {     //新增

                    if (sysdepartment.GetModel(x => x.DepartmentName == entity.DepartmentName && x.CompanyGuId == entity.CompanyGuId) == null)
                    {
                        entity.Create();

                        sysdepartment.Add(entity);


                        SuccessLogDto loginmodel = new SuccessLogDto();
                        loginmodel.data = "操作成功";
                        loginmodel._sysloginlog = sysloginlog;
                        loginmodel.loginlogmodel = new SysLoginLogEntity { Type = 1, Content = "新增部门信息" };
                        return SuccessLog(loginmodel);

                    }
                    else
                        return Error("对不起单位所在部门已存在！");

                }
                else
                {   //修改信息
                    var model = sysdepartment.GetModel(x => x.GuId == entity.GuId);
                    if (model != null)
                    {
                        model.DepartmentName = entity.DepartmentName;
                        sysdepartment.Update(model);
                    }

                    SuccessLogDto loginmodel = new SuccessLogDto();
                    loginmodel.data = "操作成功";
                    loginmodel._sysloginlog = sysloginlog;
                    loginmodel.loginlogmodel = new SysLoginLogEntity { Type = 2, Content = "修改部门信息" };
                    return SuccessLog(loginmodel);
                }


            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "SysCompany/AddDepartment", Content = ex.ToString(), Name = "新增单位部门信息" };
                return ErrorLog(model);
            }
        }

        /// <summary>
        /// 删除单位信息 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult DelCompany([FromQuery] string guid)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(guid))
                    return Error("参数错误！");

                syscompany.Delete(x => x.GuId == guid);

                SuccessLogDto loginmodel = new SuccessLogDto();
                loginmodel.data = "操作成功";
                loginmodel._sysloginlog = sysloginlog;
                loginmodel.loginlogmodel = new SysLoginLogEntity { Type = 3, Content = "删除单位信息" };
                return SuccessLog(loginmodel);
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "SysCompany/DelCompany", Content = ex.ToString(), Name = "删除单位信息" };
                return ErrorLog(model);
            }
        }


        /// <summary>
        /// 删除单位部门信息 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult DelDepartment([FromQuery] string guid)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(guid))
                    return Error("参数错误！");

                sysdepartment.Delete(x => x.GuId == guid);

                SuccessLogDto loginmodel = new SuccessLogDto();
                loginmodel.data = "操作成功";
                loginmodel._sysloginlog = sysloginlog;
                loginmodel.loginlogmodel = new SysLoginLogEntity { Type = 3, Content = "删除部门信息" };
                return SuccessLog(loginmodel);
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "SysCompany/DelDepartment", Content = ex.ToString(), Name = "删除单位部门信息" };
                return ErrorLog(model);
            }
        }



        /// <summary>
        /// 获取部门树行数据源 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult GetCompanyTree()
        {
            try
            {
                return Success(syscompany.GetCompanyTree());
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "SysCompany/GetCompanyTree", Content = ex.ToString(), Name = "获取单位、部门树形数据源" };
                return ErrorLog(model);
            }
        }



    }
}
